package com.hctforgreen.greenservice;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hctforgreen.greenservice.db.DBVideoDownloadManager;
import com.hctforgreen.greenservice.db.DBVideoManager;
import com.hctforgreen.greenservice.model.VideoTypeEntity.VideoDetailEntity.VideoPartEntity;
import com.hctforgreen.greenservice.ui.adapter.VideoHistoryAdapter;
import com.hctforgreen.greenservice.ui.widget.PullToRefreshListView;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.ThemeStoreUtil;
import com.hctforgreen.greenservice.utils.Utils;

/**
 * 视频播放记录
 * 
 * @author denghb
 * 
 */
public class VideoHistoryActivity extends ParentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video_history);
		initSkinLayout();
		initTitleButtonBar();
		initDataAndWindow();

	}

	private void initDataAndWindow() {
		final View convertView = findViewById(R.id.lyt_parent);
		final PullToRefreshListView listView = (PullToRefreshListView) convertView
				.findViewById(R.id.lst_default_list);
		final View loadView = convertView
				.findViewById(R.id.lyt_default_list_load);

		final Handler handler = new Handler() {
			@Override
			public void dispatchMessage(Message msg) {
				switch (msg.what) {

				case HctConstants.STATUS_FAILD:

					List<VideoPartEntity> data = new ArrayList<VideoPartEntity>();
					listView.setAdapter(new VideoHistoryAdapter(
							getApplicationContext(), data, listView));
					break;
				case HctConstants.STATUS_SUCCESS:
					final List<VideoPartEntity> partEntities = (List<VideoPartEntity>) msg.obj;

					if (partEntities != null && !partEntities.isEmpty()) {
						VideoHistoryAdapter historyAdapter = new VideoHistoryAdapter(
								getApplicationContext(), partEntities, listView);
						listView.setAdapter(historyAdapter);
						listView.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								playVideo(partEntities.get(arg2 - 1));
							}
						});
					}
					break;

				default:
					break;
				}
				loadView.setVisibility(View.GONE);
				listView.setVisibility(View.VISIBLE);
				super.dispatchMessage(msg);
			}
		};

		loadView.setVisibility(View.VISIBLE);
		new Thread() {
			public void run() {
				try {
					DBVideoManager dbVideoManager = new DBVideoManager(
							VideoHistoryActivity.this);
					List<VideoPartEntity> partEntities = dbVideoManager
							.queryAll();

					handler.sendMessage(handler.obtainMessage(
							HctConstants.STATUS_SUCCESS, partEntities));
				} catch (Exception e) {
					handler.sendEmptyMessage(HctConstants.STATUS_FAILD);
					e.printStackTrace();
				}

			};
		}.start();

	}

	private void playVideo(final VideoPartEntity partEntity) {

		final DBVideoDownloadManager downloadManager = new DBVideoDownloadManager(
				getApplicationContext());
		if (downloadManager.haveDownloadEntity(partEntity)) {
			Intent intent = new Intent(Intent.ACTION_VIEW);
			Uri uri = Uri.parse("file://"
					+ VideoPartEntity.makeVideoFileName(partEntity,
							getApplicationContext()));
			intent.setDataAndType(uri, "video/*");
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
		} else {
			if (!Utils.isWifiNetworkConnected(getApplicationContext())) {
				AlertDialog.Builder builder = new Builder(
						VideoHistoryActivity.this);
				builder.setMessage(
						getString(R.string.video_dialog_network_3g_wran))
						.setTitle(getString(R.string.dialog_default_title_hint))
						.setNegativeButton(
								getString(R.string.dialog_cancel_hint), null)
						.setPositiveButton(
								getString(R.string.dialog_confirm_hint),
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										Intent intent = new Intent(
												Intent.ACTION_VIEW);
										Uri uri = Uri
												.parse(partEntity.videoUrl);
										intent.setDataAndType(uri, "video/*");
										intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
										startActivity(intent);
									}
								}).create().show();
			} else {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				Uri uri = Uri.parse(partEntity.videoUrl);
				intent.setDataAndType(uri, "video/*");
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		}

	}


	private void initTitleButtonBar() {

		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.video_history_title));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

	}

	@Override
	public void onBackPressed() {

		super.onBackPressed();
	}

}
